package com.shr.code_interview;

public class Solution_58 {
    public String ReverseSentence(String str) {
    	if(str==null || str.trim().length() == 0)
    		return str;
    	StringBuffer reverseStr = reverse(str);
    	String[] strs = reverseStr.toString().split(" ");
    	StringBuffer sb = new StringBuffer();
    	for(String aStr : strs){
    		sb.append(reverse(aStr)).append(" ");
    	} 
    	return sb.substring(0, sb.length()-1);

    }

	private StringBuffer reverse(String str) {
        int mid = str.length() / 2 - 1;
        StringBuffer sb = new StringBuffer(str) ;
        for(int i = 0;i <= mid;i++){
        	int up = str.length() - 1 - i;
        	char chFirst = sb.charAt(i);
        	sb.setCharAt(i, sb.charAt(up));
        	sb.setCharAt(up, chFirst);
        }
        return sb;
	}
}
